package com.neusoft.neuiotms.dcom.converter.insurance;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.insurance.InsuranceInspectionTaskDTO;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceInspectionTaskType;
import com.neusoft.neuiotms.dcom.model.enums.InsuranceTaskStatus;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceInspectionTask;
import com.neusoft.neuiotms.dcom.model.insurance.InsuranceInspectionTaskItem;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceInspectionTaskItemRepository;
import com.neusoft.neuiotms.dcom.repository.insurance.InsuranceInspectionTaskRepository;

@Component
public class InsuranceInspectionTaskConverter
        extends AbstractConverter<InsuranceInspectionTask, InsuranceInspectionTaskDTO> {

    @Autowired
    private BaseIdGenerator insuranceInspectionTaskCodeGenerator;
    @Autowired
    private InsuranceInspectionTaskRepository insuranceInspectionTaskRepository;

    @Autowired
    private InsuranceInspectionTaskItemConverter insuranceInspectionTaskItemConverter;
    @Autowired
    private InsuranceInspectionTaskItemRepository insuranceInspectionTaskItemRepository;

    @Override
    public InsuranceInspectionTask toModel(final InsuranceInspectionTaskDTO dto) {

        InsuranceInspectionTask model;
        if (dto.isNew()) {
            model = new InsuranceInspectionTask();
            if (StringUtils.isEmpty(dto.getCode())) {
                model.setCode(this.insuranceInspectionTaskCodeGenerator.generateCode());
                model.setStartTime(dto.getStartTime());
                model.setEndTime(dto.getEndTime());
                model.setCronExp(dto.getCronExp());
                model.setUnitCode(dto.getUnitCode());
                model.setType(InsuranceInspectionTaskType.valueOf(dto.getType()));
            } else {
                model.setCode(dto.getCode());
            }
        } else {
            model = this.insuranceInspectionTaskRepository.getOne(dto.getId());
        }

        model.setExecutorCode(dto.getExecutorCode());
        model.setExecutorName(dto.getExecutorName());
        model.setProblems(dto.getProblems());
        model.setSolveSituation(dto.getSolveSituation());
        model.setStatus(InsuranceTaskStatus.valueOf(dto.getStatus()));

        // 重保服务巡检任务内容
        final List<InsuranceInspectionTaskItem> items =
                model.getInspectionTaskItem().stream().collect(Collectors.toList());

        items.forEach(k -> {
            model.getInspectionTaskItem().remove(k);
            this.insuranceInspectionTaskItemRepository.delete(k);
        });

        dto.getInspectionTaskItem().stream().map(s -> {
            final InsuranceInspectionTaskItem itemModel = this.insuranceInspectionTaskItemConverter.toModel(s);
            itemModel.setInspectionTask(model);
            return itemModel;
        }).forEach(model.getInspectionTaskItem()::add);

        return model;
    }

    @Override
    public InsuranceInspectionTaskDTO toDTO(final InsuranceInspectionTask model, final boolean forListView) {

        final InsuranceInspectionTaskDTO dto = new InsuranceInspectionTaskDTO();

        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setStartTime(model.getStartTime());
        dto.setEndTime(model.getEndTime());
        dto.setCronExp(model.getCronExp());
        dto.setExecutorCode(model.getExecutorCode());
        dto.setExecutorName(model.getExecutorName());
        dto.setUnitCode(model.getUnitCode());
        dto.setProblems(model.getProblems());
        dto.setSolveSituation(model.getSolveSituation());
        dto.setStatus(model.getStatus().name());
        dto.setType(model.getType().name());

        model.getInspectionTaskItem().stream().map(s -> this.insuranceInspectionTaskItemConverter.toDTO(s))
                .filter(k -> null != k)
                .forEach(dto.getInspectionTaskItem()::add);

        return dto;
    }

}
